﻿<html lang="en" window-frame="solid" window-icon="./view/icon/server-favicon.ico"></html>
<head>
	<title></title>
	<link rel="stylesheet" type="text/css" href="./view/css/index.css">
</head>
<body role="window-caption">
	 <div id="box">
        <div class="title_box skinbg">
                <div class="head">
                    <div class="title fl" >
                            <a href="./view/index.html" class="backicon topiconBg"></a>
                    </div>
                    <div class="setting fr">
                        <ul>
                            <li class="setbtn">
                                <span class="setbtnicon topiconBg"></span>
                                <ul class="setmess">
                                <li>
                                    <button id="passwordmode" class="passwordicon">更改密码</button>
                                </li>
                                <li>
                                    <button id="logoutmode" class="logouticon">切换账户</button>
                                </li>
                                <li>
                                    <button id="exitmode" class="exiticon">退出</button>
                                </li>
                                <li>
                                    <button id="aboutmode" class="abouticon">关于</button>
                                </li>
                                </ul>
                            </li>
                            <li>
                                <button id="minimized" class="minimizedicon topiconBg"></button>
                            </li>
                            <li>
                                <button id="closewin" class="closewinicon topiconBg"></button>
                            </li>
                        </ul>
                    </div>
                </div>
                <div class="detail_title">
                    <div class="detailicon keyicon fl"></div>
                    <div class="detailword fl">
                        <h3 class="detailh3">私钥秘钥</h3>
                        <p class="detailp">恢复简单，避免数据损坏</p>
                    </div>
                </div>
        </div>
        <div class="showmess">
            <div class="layui-form-item choice">
                <div class="layui-input-block" style="width: 100%;margin: 0;text-align: center;">
                    <button id="createface" class="layui-btn" >生成密钥对</button>
                    <button id="uploading" class="layui-btn" >上传密钥(已有秘钥)</button>
                </div>
            </div>
            <div class="showtable keyshow" > 
                <table border="0" cellpadding="0" cellspacing="0">
                    <thead>
                        <tr>
                            <th>类型</th>
                            <th>文件名</th>
                            <th>日期</th>
                            <th>操作</th>
                        </tr>
                    </thead>
                    <tbody id="tbkey" class="operation">
                            
                    </tbody>
                </table>
            </div>
        </div>
        <!-- 提示 -->
        <div id="messtip">
            <i id="messtipicon" class="successicon"></i>
            <!-- 成功 successicon 失败 defeaticon -->
            <span class="messtipword">您的操作 <span class="reminderhandle"></span>！<span class="errorhandle"></span> </span>
        </div>
        <!-- 提示 -->
    </div>
    <script type="text/tiscript" src="./view/tiscript/index.tis"></script>
        <script type="text/tiscript" src="./view/tiscript/public.tis"></script>
        <script type="text/tiscript">
                $(.choice).style["display"] = "block";
                $(.keyshow).style["display"] = "none";
                var tb = ""
                // 获取秘钥  
                   var getsecretKey = function (){
                        var tlist = view.getrsainfo()
                            tb = ""
                            if(tlist){
                                if(tlist.length > 0 ){
                                    for (var (key,v) in tlist){
                                        var type = ""
                                        if(v.ftype == "private"){ 
                                            type = "私钥"
                                        }else if(v.ftype == "public"){
                                            type = "公钥" 
                                        }
                                        tb += `
                                                <tr>
                                                    <td> `+type+`</td>
                                                    <td> `+v.fname+`</td>
                                                    <td> `+v.ftime+` </td>
                                                    <td>
                                                        <button class="layui-btn" clickname="download" ftype="`+v.ftype+`">下载</button>
                                                        <button class="layui-btn" clickname="remove" ftype="`+v.ftype+`">移除</button>
                                                    </td>
                                                </tr>
                                            ` ;                  
                                    }
                                    if(tlist.length == 1){
                                        // 按钮 组
                                        $(.choice).style["display"] = "block";
                                        $(#createface).style["display"] = "none";
                                        $(#uploading).style["display"] = "inline-block";
                                        $(.keyshow).style["display"] = "block";
                                        $(#tbkey).html = tb
                                    } else if(tlist.length > 1){
                                        // 操作按钮
                                        $(.choice).style["display"] = "none";
                                        $(#createface).style["display"] = "none";
                                        $(#uploading).style["display"] = "none";
                                        $(.keyshow).style["display"] = "block";
                                        $(#tbkey).html = tb
                                    }
                                }
                            }else{
                                $(.choice).style["display"] = "block";
                                $(#createface).style["display"] = "inline-block";
                                $(#uploading).style["display"] = "inline-block";
                                $(.keyshow).style["display"] = "none";
                                $(#tbkey).html = ""
                            }

                    }


                // 生成密钥对
                $(#createface).on("click",function(){
                    var argument = {
                            "newin":view,
                            "type":"createface",
                            "title":"生成秘钥对",
                            "text":"确认生成秘钥对",
                            "buttons":[{id:"closekey",text:"取消"},{id:"yeskey",text:"确认"}],
                            "getsecretKey":getsecretKey,
                            "success":success,
                            "error":error
                        }

                        view.dialog({
                            //加载到对话框中的URL，可加载本地文件
                            url: "./view/popup.html",
                            //对话框的x，y坐标
                            x: 0,
                            y: 0,
                            //窗口的宽度，高度
                            width: 272,
                            height: 170,
                            //是否是客户区，如果为真，那么x，y，width，height作为客户区坐标，而不是窗口
                            client: false,
                            //对话框 参数
                            parameters: argument,
                            //对话框标题
                            caption: "新对话框",
                            //相对于屏幕窗口对齐
                            alignment: 1,
                        });




                    
                    
                })
                // 上传 秘钥
                $(#uploading).on("click",function(){
                     //文件筛选项
                    const filter = "Pem Files (*.pem)";
                    //默认后缀
                    const defaultExt = "pem";
                    //标题
                    const caption = "选择秘钥文件";
                    var file = view.selectFile(#open,filter, defaultExt, "", caption );
                    if(file){
                        var len = file.split("/").length - 1 
                        var filename = file.split("/")[len]
                        var rendata = view.uploadkey(filename)
                    
                       if (rendata.success) {
                            success("上传成功",getsecretKey)
                        }else{
                            error(rendata.msg)
                        }
                    }
                })
                  
              
                self.ready = function() {
                    getsecretKey()
                }
                event click $(button) (evt, element) {  
                    if(this.attributes["clickname"] == "download"){
                        var ftype = this.attributes["ftype"]
                        download(ftype)
                    }else if(this.attributes["clickname"] == "remove"){
                        var ftype = this.attributes["ftype"]
                        remove(ftype)	
                    } 
                }
                function download(type){
                    var folder = view.selectFolder("请选择下载目录","./");
                    if(type && folder){
                        var downloadres = view.downloadkey(type,folder)
                        if (downloadres.success){
                            success("下载完成",getsecretKey)
                        }else{
                            error(downloadres.msg)
                        }  
                    }  
                }
                function remove(type){
                        var argument = {
                            "newin":view,
                            "type":"hintkey",
                            "title":"确认删除",
                            "text":"删除时请确认已保存秘钥",
                            "buttons":[{id:"closekey",text:"下载并移除"},{id:"yeskey",text:"确认移除"}],
                            "ftype":type,
                            "getsecretKey":getsecretKey,
                            "success":success,
                            "error":error
                        }

                        view.dialog({
                            //加载到对话框中的URL，可加载本地文件
                            url: "./view/popup.html",
                            //对话框的x，y坐标
                            x: 0,
                            y: 0,
                            //窗口的宽度，高度
                            width: 272,
                            height: 170,
                            //是否是客户区，如果为真，那么x，y，width，height作为客户区坐标，而不是窗口
                            client: false,
                            //对话框 参数
                            parameters: argument,
                            //对话框标题
                            caption: "新对话框",
                            //相对于屏幕窗口对齐
                            alignment: 1,
                        });
                }

       </script>
	</body>
</html>